<template>
  <div class="app-container">
    机器人:{{ robotName }},id:{{ robotId }},策略:{{ strategyId }},账户:{{ exchangeId }}
    <div>
      参数列表
      <el-form ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item v-for="item in argInfos" :key="item.Id" :label="item.Name">
          <el-input v-model="item.DefaultVal" /><span>{{ item.Type }},{{ item.Remark }}</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">更新参数</el-button>
          <el-button v-if="false" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import sapi from '@/api/sysApi'

export default {
  data() {
    return {
      loading: false,
      robotId: '',
      robotName: '',
      robot: {},
      strategy: {},
      argInfos: '',
      strategyId: '',
      exchangeId: '',
      periodList: [
        { loading: false, name: '1m', period: 60, sign: 'OneMinute,' },
        { loading: false, name: '"3m"', period: 3 * 60, sign: 'ThreeMinutes,' },
        { loading: false, name: '"5m"', period: 5 * 60, sign: 'FiveMinutes,' },
        { loading: false, name: '"15m"', period: 15 * 60, sign: 'FifteenMinutes,' },
        { loading: false, name: '"30m"', period: 30 * 60, sign: 'ThirtyMinutes,' },
        { loading: false, name: '"1h"', period: 3600, sign: 'OneHour,' },
        { loading: false, name: '"2h"', period: 2 * 3600, sign: 'TwoHours,' },
        { loading: false, name: '"4h"', period: 4 * 3600, sign: 'FourHours,' },
        { loading: false, name: '"6h"', period: 6 * 3600, sign: 'SixHours,' },
        { loading: false, name: '"8h"', period: 8 * 3600, sign: 'EightHours,' },
        { loading: false, name: '"12h"', period: 12 * 3600, sign: 'TwelveHours,' },
        { loading: false, name: '"1d"', period: 24 * 3600, sign: 'OneDay,' },
        { loading: false, name: '"3d"', period: 3 * 24 * 3600, sign: 'ThreeDays,' },
        { loading: false, name: '"1w"', period: 7 * 24 * 3600, sign: 'OneWeek,' },
        { loading: false, name: '"1M"', period: 30 * 24 * 3600, sign: 'OneMonth' }
      ]
    }
  },
  created() {
    this.robotId = this.$route.query.robotId
    this.robotName = this.$route.query.robotName
    console.log(this.robotId)
    sapi.getRobotList().then(resp => {
      this.robot = resp.data.filter(p => {
        var rr = (p.id + '') === (this.robotId + '')
        console.log(rr, p, this.robotId)
        return rr
      })[0]
      console.log(this.robot)
      if (this.robot) {
        this.strategyId = this.robot.strategyId
        this.exchangeId = this.robot.exchangeId
        sapi.getStrategyList().then(resp2 => {
          this.strategy = resp2.data.filter(p => p.id === this.robot.strategyId)[0]
          console.log(this.strategy)
          if (this.strategy && this.strategy.argInfosStr) {
            this.argInfos = JSON.parse(this.strategy.argInfosStr)
            if (this.robot.strategyArgs) {
              var sargs = JSON.parse(this.robot.strategyArgs)
              this.argInfos.forEach(p => {
                if (p.Name in sargs) {
                  p.DefaultVal = sargs[p.Name]
                }
              })
            }
          }
        })
      }
    })
  },
  mounted() {

  },
  methods: {
    rest(item) {
      sapi.RestartRobot(item.id).then((p) => {
        if (p) item.status = 1
      })
    },
    stop(item) {
      sapi.StopRobot(item.id).then((p) => {
        if (p) item.status = 0
      })
    },
    submitForm() {
      sapi.editRobotStrategyArgs(this.robotId, this.argInfos).then(resp => {
        this.$alert('设置成功')
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

